<?php

namespace App\Listeners;

use App\Events\OrderShipped;
use App\Mail\FindPassword;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;

class SendShipmentNotification implements ShouldQueue
{

    /**
     * 任务应该发送到的队列的名称
     * @var string|null
     */
    public $queue = 'eventListeners';

    /**
     * Create the event listener.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * @param OrderShipped $event
     * @return bool
     */
    public function handle(OrderShipped $event)
    {
        try {
            $user = $event->user;

            $pwd = str_random(6);
            $attach = storage_path('app/test.txt');
            \Mail::to($user->email)->send(new FindPassword($pwd, $attach));
        } catch (\Exception $e) {
            return false;
        }

        return true;
    }

    /**
     * 处理任务失败
     * @param  \App\Events\OrderShipped  $event
     * @param  \Exception  $exception
     * @return void
     */
    public function failed(OrderShipped $event, $exception)
    {
        \Log::info(json_encode($exception));
    }
}
